//
//  XMGTopicCell.m
//  BuDeJie19
//
//  Created by hjj on 2016/12/3.
//  Copyright © 2016年 hjjuny. All rights reserved.
//

#import "XMGTopicCell.h"
#import "XMGTopTopicView.h"
#import "XMGTopicItem.h"
#import "XMGTopicViewModel.h"
#import "XMGPictureTopicView.h"
#import "XMGVideoTopicView.h"
#import "XMGVoiceTopicView.h"
#import "XMGCommentView.h"
#import "XMGBottomView.h"

@interface XMGTopicCell()
@property (nonatomic, weak) XMGTopTopicView *topView;
@property (nonatomic, weak) XMGPictureTopicView *pictureView;
@property (nonatomic, weak) XMGVideoTopicView *videoView;
@property (nonatomic, weak) XMGVoiceTopicView *voiceView;
@property (nonatomic, weak) XMGCommentView *commentView;
@property (nonatomic, weak) XMGBottomView *bottomView;

@end

@implementation XMGTopicCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        NSLog(@"%s",__func__);
    }
    return self;
}

- (void)setFrame:(CGRect)frame
{
    frame.origin.y += 10;
    frame.size.height -= 10;
    [super setFrame:frame];
}

// Cell比较特殊，通过initWithStyle
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UIImage *image = [UIImage imageNamed:@"mainCellBackground"];
        image = [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
        [self setBackgroundView:[[UIImageView alloc] initWithImage:image]];
        
        // 设置顶部View内容，设置topView，设置Cell尺寸
        // 顶部
        XMGTopTopicView *topView = [XMGTopTopicView viewForXib];
        [self.contentView addSubview:topView];
        _topView = topView;
        
        // 中间（视频，声音，图片）
        // 图片
        XMGPictureTopicView *pictureView = [XMGPictureTopicView viewForXib];
        [self.contentView addSubview:pictureView];
        _pictureView = pictureView;
        
        // 视频
        XMGVideoTopicView *videoView = [XMGVideoTopicView viewForXib];
        [self.contentView addSubview:videoView];
        _videoView = videoView;
        
        // 声音
        XMGVoiceTopicView *voiceView = [XMGVoiceTopicView viewForXib];
        [self.contentView addSubview:voiceView];
        _voiceView = voiceView;
        
        // 最热评论
        XMGCommentView *commentView = [XMGCommentView viewForXib];
        [self.contentView addSubview:commentView];
        _commentView = commentView;
        
        // 底部
        XMGBottomView *bottomView = [XMGBottomView viewForXib];
        [self.contentView addSubview:bottomView];
        _bottomView = bottomView;
         
    }
    return self;
}
- (void)setVm:(XMGTopicViewModel *)vm
{
    _vm = vm;
    
    // 顶部View
    _topView.item = vm.item;
    _topView.frame = vm.topViewFrame;
    
    // 中间View
    if (vm.item.type == XMGTopicItemTypePicture) { // 图片
        _pictureView.item = vm.item;
        _pictureView.frame = vm.middleViewFrame;
        
        _videoView.hidden = YES;
        _voiceView.hidden = YES;
        _pictureView.hidden = NO;
        
    } else if (vm.item.type == XMGTopicItemTypeVideo) { // 视频
        _videoView.item = vm.item;
        _videoView.frame = vm.middleViewFrame;
        
        _videoView.hidden = NO;
        _pictureView.hidden = YES;
        _voiceView.hidden = YES;
        
    } else if (vm.item.type == XMGTopicItemTypeVocie) { // 声音
        _voiceView.item = vm.item;
        _voiceView.frame = vm.middleViewFrame;
        
        _videoView.hidden = YES;
        _pictureView.hidden = YES;
        _voiceView.hidden = NO;
    } else { // 段子
        _videoView.hidden = YES;
        _pictureView.hidden = YES;
        _voiceView.hidden = YES;
    }
    
    // 设置最热评论
    if (vm.item.topComment) {
        _commentView.hidden = NO;
        
        _commentView.item = vm.item;
        _commentView.frame = vm.commentViewFrame;
        
    } else {
        _commentView.hidden = YES;
    }
    
    // 设置底部View
    _bottomView.item = vm.item;
    _bottomView.frame = vm.bottomViewFrame;
}



@end
